package com.winit.test;

import com.winit.base.BaseInsert;
import com.winit.base.BaseParam;
import com.winit.base.ParamType;
import com.winit.mycat.MycatConnUtil;

import java.sql.Connection;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.winit.test.Delete.*;

public class Insert {

    /**
     * 根据int分片，一致哈西
     *
     * @param conn
     */
    static void InsertA(Connection conn) throws Exception {
        System.out.println("=================InsertA================");
        String a_sql = "INSERT INTO a(column_2,column_3,column_4) VALUES (?, ?,?)";
        try {
            conn.setAutoCommit(false);
            for (int i = 0; i < 100; i++) {
//                Thread.sleep(1000);
                List<BaseParam> list = new ArrayList<>();
                list.add(new BaseParam(1, ParamType.INT, i));
                list.add(new BaseParam(2, ParamType.DATE, new Date(System.currentTimeMillis())));
                list.add(new BaseParam(3, ParamType.STRING, "打发的====" + i + "====aaaaaa"));
                BaseInsert.insertT(conn, a_sql, list);
            }
            conn.commit();
        }catch (Exception e){
            conn.rollback();
            e.printStackTrace();
        }finally {
            conn.setAutoCommit(true);
            System.out.println("=================InsertA================");
        }
    }

    /**
     * 根据int分片，一致哈西
     *
     * @param conn
     */
    static void InsertB(Connection conn) throws Exception{
        System.out.println("=================InsertB================");
        String b_sql = "INSERT INTO b(column_2,column_7,column_8,column_15) VALUES (?, ?,?,?)";
        for (int i = 0; i < 100; i++) {
            Thread.sleep(500);
            java.util.Date now = new java.util.Date();
            System.out.println("===========" + now.getTime());
            List<BaseParam> list = new ArrayList<>();
            list.add(new BaseParam(1, ParamType.INT, i));
            list.add(new BaseParam(2, ParamType.DATETIME, new Timestamp(now.getTime())));
            list.add(new BaseParam(3, ParamType.STRING, "打发的====" + i + "====aaaaaa"));
            list.add(new BaseParam(4, ParamType.STRING, String.valueOf(now.getTime())));
            BaseInsert.insertT(conn, b_sql, list);
        }
        System.out.println("=================InsertB================");
    }

    /**
     * 根据string分片，范围
     *
     * @param conn
     */
    static void InsertC(Connection conn) throws Exception {
        System.out.println("=================InsertC================");
        String c_sql = "INSERT INTO c(column_12,column_13,column_14,column_15) VALUES (?, ?,?,?)";
        for (int i = 0; i < 100; i++) {
            Thread.sleep(500);
            java.util.Date now = new java.util.Date();
            System.out.println("===========" + now.getTime());
            List<BaseParam> list = new ArrayList<>();
            list.add(new BaseParam(1, ParamType.INT, i));
            list.add(new BaseParam(2, ParamType.DATETIME, new Timestamp(now.getTime())));
            list.add(new BaseParam(3, ParamType.STRING, "打发的====" + i + "====aaaaaa"));
            list.add(new BaseParam(4, ParamType.STRING, String.valueOf(now.getTime())));
            BaseInsert.insertT(conn, c_sql, list);
        }
        System.out.println("=================InsertC================");
    }

    static void InsertD(Connection conn) {
        System.out.println("=================InsertD================");
        String d_sql = "INSERT INTO global_d(column_22,column_23,column_24) VALUES (?, ?,?)";
        for (int i = 0; i < 10; i++) {
            List<BaseParam> list = new ArrayList<>();
            list.add(new BaseParam(1, ParamType.INT, i));
            list.add(new BaseParam(2, ParamType.DATE, new Date(System.currentTimeMillis())));
            list.add(new BaseParam(3, ParamType.STRING, "打发的====" + i + "====aaaaaa"));
            BaseInsert.insertT(conn, d_sql, list);
        }
        System.out.println("=================InsertD================");
    }


    public static void main(String... args) throws Exception {
        Connection connection = null;
//       mycat
        connection = MycatConnUtil.getMycatConn();
        long a = new java.util.Date().getTime();
        System.out.println("修改 autopartition-long.txt：\r\n");
        System.out.println("echo " + String.valueOf(a) + "-" + String.valueOf(a + 50000) + "=0 > /usr/local/Mycat/conf/autopartition-long.txt\r\n");
        System.out.println("echo " + String.valueOf(a + 50001) + "-" + String.valueOf(a + 150000) + "=1 >> /usr/local/Mycat/conf/autopartition-long.txt && mycat restart");

        if (connection != null) {

            DeleteAAll(connection);
//            DeleteBAll(connection);
//            DeleteCAll(connection);
//            DeleteDAll(connection);

            InsertA(connection);
//            InsertB(connection);
//            InsertC(connection);
//            InsertD(connection);

            connection.close();
        }
    }
}
